package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.ManagerDao;
import com.woniuxy.entity.RbacManagerDTO;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.RbacManagerService;
import com.woniuxy.util.SecurityCryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName RbacManagerServiceImpl
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/8/22 9:48
 * @Version 1.0
 */
@Service
public class RbacManagerServiceImpl implements RbacManagerService{

    @Autowired
    private ManagerDao managerDao;

    public ManagerDao getManagerDao() {
        return managerDao;
    }

    public void setManagerDao(ManagerDao managerDao) {
        this.managerDao = managerDao;
    }

    @Override
    public ResponseEntity selectAll(int currentPage,int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> list= managerDao.selectAll();
        PageInfo pageInfo = new PageInfo(list);
        return new ResponseEntity(200, "成功", pageInfo);
    }

    @Override
    @Transactional
    public ResponseEntity addUser(RbacManagerDTO rbacManager) {
        int count = managerDao.findCountByNameOrId(rbacManager);
        if(count>0){
            return new ResponseEntity(500,"新增失败,账号同名！",null);
        }else {
            //数据库没有账号的时候 才允许新增
            String s = SecurityCryUtils.generateEncryptedPassword(rbacManager.getPassword(), rbacManager.getAccount());
            rbacManager.setPassword(s);
            int i = managerDao.addUser(rbacManager);
            if (i>0){
                return new ResponseEntity(200,"成功新增",null);
            }else {
                return new ResponseEntity(500,"新增失败",null);
            }
        }
    }

    @Override
    public void updateUser(User user) {
        managerDao.updateUser(user);
    }

    @Override
    public int deleteUserById(int id) {
        int rows = managerDao.deleteUserById(id);
        return rows;
    }

    @Override
    public List<User> findUserByName(String username) {
        return managerDao.findUserByName(username);
    }

    @Override
    public ResponseEntity editOne(User user) {
        if (user.getId()!=0){
            int count = managerDao.findCountByNameOrId(user);
            if(count>1){
                return new ResponseEntity(500,"修改失败,账号同名！",null);
            }else{
                int i = managerDao.updateUser(user);
                if (i>0){
                    return new ResponseEntity(200,"成功修改",null);
                }else {
                    return new ResponseEntity(500,"修改失败",null);
                }
            }
        }else{
            return new ResponseEntity(500,"id不能没有！",null);
        }
    }

    @Override
    public ResponseEntity search(int currentPage, int pageSize,String account,String roleName) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> list= managerDao.search(account,roleName);
        PageInfo pageInfo = new PageInfo(list);
        return new ResponseEntity(200, "", pageInfo);
    }

    @Override
    public User findOneByName(String username) {
        return managerDao.findOneByName(username);
    }

    @Override
    public ResponseEntity editType(int id) {
        int i = managerDao.editType(id);
        if (i>0){
            return new ResponseEntity(200,"成功更新"+id+"这条数据状态",null);
        }else {
            return new ResponseEntity(500,"更新失败",null);
        }
    }

    @Override
    public ResponseEntity initPwd(int id) {
        User user = managerDao.findOneById(id);
        String newPassword = SecurityCryUtils.generateEncryptedPassword("123456", user.getAccount());
        int i = managerDao.updateInitPwd(id,newPassword);
        if (i>0){
            return new ResponseEntity(200,"成功更新"+id+"这条密码成功",null);
        }else {
            return new ResponseEntity(500,"更新失败",null);
        }
    }

    @Override
    public ResponseEntity updateRole(RbacManagerDTO rbacManagerDTO) {
        if (rbacManagerDTO.getId()!=0){
            int count = managerDao.findCountByNameOrId(rbacManagerDTO);
            if(count>1){
                return new ResponseEntity(500,"修改失败,账号同名！",null);
            }else{
                String newP = SecurityCryUtils.generateEncryptedPassword(rbacManagerDTO.getPassword(), rbacManagerDTO.getAccount());
                rbacManagerDTO.setPassword(newP);
                int i = managerDao.updateRole(rbacManagerDTO);
                if (i>0){
                    return new ResponseEntity(200,"成功更新’"+rbacManagerDTO.getAccount()+"‘用户的角色",null);
                }else {
                    return new ResponseEntity(500,"更新失败",null);
                }
            }
        }else{
            return new ResponseEntity(500,"id不能没有！",null);
        }
    }
}
